﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using Reborn_Server.App1AppHandler;
using Reborn_WorldServer;

namespace App1AppHandler
{
    public class Weapon : Equipment, ICloneable
    {
        public int DmgLow = 0;
        public int DmgHigh = 0;
        public decimal Speed = 0;
        public int Accuracy = 0;
        public int Defense = 0;
        public int StamCost = 0;

        public bool Melee = false;
        public bool Range = false;
        public bool RangeThrows = false;
        public bool ThrowReturns = false;
        public int RangeDistance = 0;

        public string Skill = "";

        public SpellIcon WeaponEffect = null;
        public int WeaponEffectLevel = -1;
        public int WeaponEffectInt = -1;
        public int WeaponEffectWis = -1;

        public Weapon(AppHandler2 appHandler): base(appHandler) { }

        protected void copyProperties(Weapon copyTo)
        {
            base.copyProperties(copyTo);

            copyTo.DmgLow = this.DmgLow;
            copyTo.DmgHigh = this.DmgHigh;
            copyTo.Speed = this.Speed;
            copyTo.Accuracy = this.Accuracy;
            copyTo.Defense = this.Defense;
            copyTo.StamCost = this.StamCost;
            copyTo.Melee = this.Melee;
            copyTo.Range = this.Range;
            copyTo.RangeThrows = this.RangeThrows;
            copyTo.RangeDistance = this.RangeDistance;
            copyTo.ThrowReturns = this.ThrowReturns;
            copyTo.Skill = this.Skill;
        }

        public override string toXML(Character sendingToCharacter)
        {
            string result = "";

            StringWriter stringWriter = new StringWriter();
            XmlTextWriter xmldoc = new XmlTextWriter(stringWriter);

            //Loot Attributes
            xmldoc.WriteStartElement("i");
            xmldoc.WriteAttributeString("a", ID.ToString());
            xmldoc.WriteAttributeString("b", GameUID.ToString());
            xmldoc.WriteAttributeString("c", Name.ToString());
            xmldoc.WriteAttributeString("d", ClassName.ToString());
            xmldoc.WriteAttributeString("e", ItemType.ToString());
            xmldoc.WriteAttributeString("f", ImageURL.ToString());
            xmldoc.WriteAttributeString("g", CanConsume.ToString());
            xmldoc.WriteAttributeString("h", Bag.ToString());
            xmldoc.WriteAttributeString("i", Belt.ToString());
            xmldoc.WriteAttributeString("j", Equip.ToString());
            xmldoc.WriteAttributeString("k", Description.ToString());
            xmldoc.WriteAttributeString("l", PriceLow.ToString());
            xmldoc.WriteAttributeString("m", PriceHigh.ToString());

            xmldoc.WriteAttributeString("n", Weight.ToString());
            xmldoc.WriteAttributeString("o", BoundTo.ToString());
            xmldoc.WriteAttributeString("p", Lore.ToString());


            //Equipment Attributes
            xmldoc.WriteAttributeString("r", Strength.ToString());
            xmldoc.WriteAttributeString("s", Intelligence.ToString());
            xmldoc.WriteAttributeString("t", Dexterity.ToString());
            xmldoc.WriteAttributeString("u", Agility.ToString());
            xmldoc.WriteAttributeString("v", Wisdom.ToString());
            xmldoc.WriteAttributeString("w", Slot.ToString());

            xmldoc.WriteAttributeString("x", Armor.ToString());

            xmldoc.WriteAttributeString("y", HP.ToString());
            xmldoc.WriteAttributeString("z", HP_Regen.ToString());
            xmldoc.WriteAttributeString("A", Stam.ToString());
            xmldoc.WriteAttributeString("B", Stam_Regen.ToString());
            xmldoc.WriteAttributeString("C", Mana.ToString());
            xmldoc.WriteAttributeString("D", Mana_Regen.ToString());

            xmldoc.WriteAttributeString("E", ResistFire.ToString());
            xmldoc.WriteAttributeString("F", ResistIce.ToString());
            xmldoc.WriteAttributeString("G", ResistHoly.ToString());
            xmldoc.WriteAttributeString("H", ResistUnholy.ToString());
            xmldoc.WriteAttributeString("I", ResistMagic.ToString());

            xmldoc.WriteAttributeString("V", ResistMagic.ToString());

            //Weapon Attributes
            xmldoc.WriteAttributeString("L", DmgLow.ToString());
            xmldoc.WriteAttributeString("M", DmgHigh.ToString());
            xmldoc.WriteAttributeString("N", Speed.ToString());
            xmldoc.WriteAttributeString("O", Accuracy.ToString());
            xmldoc.WriteAttributeString("P", Defense.ToString());

            xmldoc.WriteAttributeString("Q", Melee.ToString());

            if (sendingToCharacter.weaponSkillManager.hasExtendedReach(this) == true)
            {
                xmldoc.WriteAttributeString("R", (true).ToString()); //Range flag
                xmldoc.WriteAttributeString("S", (false).ToString()); //Throw flag
                xmldoc.WriteAttributeString("T", "1"); //Range distance
            }
            else
            {
                xmldoc.WriteAttributeString("R", Range.ToString());
                xmldoc.WriteAttributeString("S", RangeThrows.ToString());
                xmldoc.WriteAttributeString("T", RangeDistance.ToString());
            }

            xmldoc.WriteAttributeString("U", (WeaponEffect != null).ToString());
          

            //Equipment Requirements
            xmldoc.WriteStartElement("J");
            foreach (LevelRequirement levelRequirement in RequirementsAll)
            {
                xmldoc.WriteRaw(levelRequirement.toXML());
            }
            xmldoc.WriteEndElement();

            xmldoc.WriteStartElement("K");
            foreach (LevelRequirement levelRequirement in RequirementsAny)
            {
                xmldoc.WriteRaw(levelRequirement.toXML());
            }
            xmldoc.WriteEndElement();



            xmldoc.WriteEndElement();
            xmldoc.Flush();

            xmldoc.Close();

            stringWriter.Flush();

            result = stringWriter.ToString();

            return result;
        }


        #region ICloneable Members



        public override object Clone()
        {
            Weapon clonedWeapon = new Weapon(Managers.GameHandler);
            copyProperties(clonedWeapon);

            return clonedWeapon;
        }

        #endregion
    }
}
